package com.enqualcomm.kids.mvp.terminalinfo;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.Constants;
import com.enqualcomm.kids.view.FenceNameMyInputFilter;
import com.enqualcomm.kids.view.MyInputFilter;

import java.util.Locale;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;
import common.utils.PromptUtil;

/**
 * Created by Sunzhigang on 2015/12/21.
 */
public class MyEditDialog extends Dialog {
    private int type = -1;
    private TerminalInfoCallBack callBack;
    private Context context;
    private EditText myEdit;
    private String content;
    private String watchName;

    /**
     * @param context
     * @param type    1.关系
     *                3：名字
     *                4：手机号
     *                5:电子围栏名称
     */
    public MyEditDialog(Context context, int type, String content, TerminalInfoCallBack callBack) {
        super(context);
        this.context = context;
        this.type = type;
        this.callBack = callBack;
        this.content = content;
    }

    public MyEditDialog(Context context, int type, String content, String watchName, TerminalInfoCallBack callBack) {
        super(context);
        this.context = context;
        this.type = type;
        this.callBack = callBack;
        this.content = content;
        this.watchName = watchName;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_myedit);

        setDialogAttributes();
        initDialogView();
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void initDialogView() {
        //1.初始化标题
        TextView titleText = (TextView) findViewById(R.id.titleText);
        myEdit = (EditText) findViewById(R.id.myEdit);

//        //2.初始化编辑框
//        String str = tv.getText().toString();

        switch (type) {
            case 1:
                titleText.setText(context.getString(R.string.relation));
                if (content.length() != 0) {
                    myEdit.setText(content);
                    myEdit.setSelection(content.length());
                }
                if (watchName != null) {
//                    MyFrontUtil.initTextViewText(context, titleText, watchName, context.getString(R.string.relation));
                    MyFrontUtil.initTextViewText(context, titleText, watchName,"我是","的",2,watchName.length()+2);
                }
                myEdit.setFilters(new InputFilter[]{new MyInputFilter()});
                break;
            case 3:
                titleText.setText(context.getString(R.string.name));
                myEdit.setText(content);
                myEdit.setSelection(content.length());
                if (watchName != null) {
                    MyFrontUtil.initTextViewText(context, titleText, watchName, context.getString(R.string.name));
                }
                myEdit.setFilters(new InputFilter[]{new MyInputFilter()});
                break;
            case 4:
                titleText.setText(context.getString(R.string.watch_phone_number));
                myEdit.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                myEdit.setText(content);
                myEdit.setSelection(content.length());
                if (watchName != null) {
                    MyFrontUtil.initTextViewText(context, titleText, watchName, context.getString(R.string.watch_phone_number));
                }
                InputFilter.LengthFilter lengthFilter3 = new InputFilter.LengthFilter(24);
                myEdit.setFilters(new InputFilter[]{lengthFilter3});
                break;
            case 5:
                titleText.setText(context.getString(R.string.edit_name_dialog_title));
                myEdit.setText(content);
                myEdit.setSelection(content.length());
                if (watchName != null) {
                    MyFrontUtil.initTextViewText(context, titleText, watchName, context.getString(R.string.edit_name_dialog_title));
                }
                if (Locale.SIMPLIFIED_CHINESE.equals(context.getResources().getConfiguration().locale)) {
                    InputFilter.LengthFilter lengthFilter = new InputFilter.LengthFilter(4);
                    myEdit.setFilters(new InputFilter[]{lengthFilter});
                } else {
                    myEdit.setFilters(new InputFilter[]{new FenceNameMyInputFilter()});
                }

                break;
        }


        //3.初始化确定，取消按钮
        findViewById(R.id.cancelBtn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    //隐藏输入法
//                    inputMethodManager.hideSoftInputFromWindow(myEdit.getWindowToken(), 0);
                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.sureBtn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    String str = myEdit.getText().toString().trim();
                    if (!"".equals(str)) {
                        if (type == 4 && !str.matches(Constants.WATCH_PHONENUMBER_REGEX)) {
                            PromptUtil.toast(getContext(), R.string.error_phonenumber_input);
                            return false;
                        }
                        callBack.inputOver(str);
                        dismiss();
                    } else {
                        PromptUtil.toast(getContext(), R.string.input_empty);
                    }
                }
                return false;
            }
        });

    }
}
